package com.frog.lock.starter;

//import com.frog.lock.starter.config.KlockConfig;
import com.frog.lock.starter.core.BusinessKeyProvider;
import com.frog.lock.starter.core.KlockAspectHandler;
import com.frog.lock.starter.core.LockInfoProvider;
import com.frog.lock.starter.lock.LockFactory;
import io.netty.channel.nio.NioEventLoopGroup;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
//@ConditionalOnProperty(prefix = KlockConfig.PREFIX, name = "enable", havingValue = "true", matchIfMissing = true)
@AutoConfigureAfter(RedisAutoConfiguration.class)
//@EnableConfigurationProperties(KlockConfig.class)
@Import({KlockAspectHandler.class})
public class KlockAutoConfiguration {

//    @Autowired
//    private KlockConfig klockConfig;

//    @Bean(destroyMethod = "shutdown")
//    @ConditionalOnMissingBean
//    RedissonClient redisson() throws Exception {
//        Config config = new Config();
//        if(klockConfig.getClusterServer()!=null){
//            config.useClusterServers().setPassword(klockConfig.getPassword())
//                    .addNodeAddress(klockConfig.getClusterServer().getNodeAddresses());
//        }else {
//            config.useSingleServer().setAddress(klockConfig.getAddress())
//                    .setDatabase(klockConfig.getDatabase())
//                    .setPassword(klockConfig.getPassword());
//        }
//        config.setEventLoopGroup(new NioEventLoopGroup());
//        return Redisson.create(config);
//    }

    @Bean
    public LockInfoProvider lockInfoProvider(){
        return new LockInfoProvider();
    }

    @Bean
    public BusinessKeyProvider businessKeyProvider(){
        return new BusinessKeyProvider();
    }

    @Bean
    public LockFactory lockFactory(){
        return new LockFactory();
    }
}
